<?php
namespace Todo\Service;
use Todo\Model\Sys\LogsModel;

/**
 * Created by PhpStorm.
 * User: ypp
 * Date: 2016/6/19
 * Time: 14:43
 */
class LogService extends BaseService
{
    private static $_INSTANCE = null;
    
    private $__model = null;

    public function __construct()
    {
        $this->__model = new LogsModel();
    }

    public static function getInstance(){
        if(!self::$_INSTANCE) self::$_INSTANCE = new LogService();
        return self::$_INSTANCE;
    }

    /**
     * 通用操作日志
     */
    public function log(){
        $info = array(
            'user_id' => LoginService::getInstance()->getCurrentUser('id'),
            'method'  => strtolower($_SERVER["REQUEST_METHOD"]),
            'url'      => $this->getLogUrl(),
            'log_time' => NOW_TIME,
            'log_ip'   => get_client_ip()
        );
        if($info['method'] == 'post') $info['data'] = json_encode($_POST);
        else  $info['data'] = json_encode($_REQUEST);
        $this->__model->create($info) && $this->__model->add();
    }

    /**
     * 自定义操作日志
     * @param array $data 数据
     */
    public function record($data){
        $info = array(
            'user_id' => LoginService::getInstance()->getCurrentUser('id'),
            'method'  => strtolower($_SERVER["REQUEST_METHOD"]),
            'url'      => $this->getLogUrl(),
            'data'     => json_encode($data),
            'log_time' => NOW_TIME,
            'log_ip'   => get_client_ip()
        );
        $this->__model->create($info) && $this->__model->add();
    }

    /**
     * 登录日志
     */
    public function login($account){
        $info = array(
            'user_id' => LoginService::getInstance()->getCurrentUser('id'),
            'method'  => 'login',
            'url'      => $this->getLogUrl(),
            'data'     => $account,
            'log_time' => NOW_TIME,
            'log_ip'   => get_client_ip()
        );
        $this->__model->create($info) && $this->__model->add();
    }

    /**
     * 当前请求的URL
     * @return mixed
     */
    protected function getLogUrl(){
        return __INFO__;
    }
}